home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 16585 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  5.7 KB

  1. Path: ix.netcom.com!news
  2. From: giuliano@ix.netcom.com(Giuliano Carlini)
  3. Newsgroups: comp.lang.c++
  4. Subject: Why don't you use garbage collection
  5. Date: 11 Apr 1996 06:58:08 GMT
  6. Organization: Netcom
  7. Message-ID: <4kiai0$mjd@dfw-ixnews8.ix.netcom.com>
  8. NNTP-Posting-Host: lbx-ca7-14.ix.netcom.com
  9. X-NETCOM-Date: Thu Apr 11  1:58:08 AM CDT 1996
  10.  
  11. I'm a long time proponent of using garbage collection in C and C++
  12. programs, and I'm curious:
  13.     - How many others are there?
  14.     - Why don't most C/C++ programmers use it?
  15. I'm particularly interested in finding out why most C/C++ don't use it.
  16. While I have my own theories - which I'll describe below - I'm
  17. interested
  18. in finding out more directly from those who are against it. I've spent
  19. a
  20. good part of the past 4 years trying to convince people to use it, and
  21. I've got a handle on why the small group of people I associate with are
  22. not using it, but I'd like to find out more about why the C/C++
  23. community
  24. as a whole does not.
  25.  
  26. I hope that with this information, I'll be able to perfect a more
  27. convincing presentation for why the default should be to use garbage
  28. collection, and for why explicit calls to free/delete should be used
  29. only
  30. in the rare cases for which garbage collection is inappropriate.
  31.  
  32. What follows is my belief for why garbage collection is so little used.
  33. Feel free to respond to anything I say below, but please, first respond
  34. to the questions above. I believe that most people don't use garbage
  35. collection because either they:
  36.     - don't know what it is
  37.     - don't know it can be used with C/C++.
  38.     - are misinformation
  39.     - are biased against it by the C/C++ culture
  40. In my experience, most C/C++ programmers either don't know what garbage
  41. collection is, or don't know that it can be used with C/C++. After all,
  42. no major C/C++ compiler includes a garbage collector. At least, as far
  43. as I know. I hope I'm wrong, and that someone can correct me. But even
  44. after, I tell them what it is, and that it can be used with C++, almost
  45. everyone still rejects it. 
  46.  
  47. At first, most offer technical reasons for rejecting it. Almost all are
  48. based on misinformation, since garbage collection is usable and
  49. benificial
  50. for the vast majority of systems. The most often mentioned is that
  51. garbage
  52. collection is too inefficient. Either it uses too much CPU, or too much
  53. memory. Fortunately, this is easy to rebut. The research literature
  54. gives
  55. ample evidence that modern garbage collectors for C/C++ use around the
  56. same CPU time as programs that explicitly call delete/free. My personal
  57. experience is that typical programs that call delete/free use about the
  58. same amount of memory as garbage collected programs. In order to impose
  59. the predictability needed to permit calls to delete, they are forced to
  60. do deep copies of complex data structures. This uses a great deal of
  61. memory. Garbage collected programs need use only a single copy of the
  62. data, and this compensates for the additional memory taken up by
  63. unreachable objects.
  64.  
  65. Another reason often given is that garbage collection introduces long
  66. pause times that are acceptable to users. This was certainly true of
  67. garbage collectors at one time, but modern collectors allow the client
  68. program to employ several techniques to eliminate pause times. The most
  69. sophisticated is to operate in a separate thread. Thus while the
  70. garbage
  71. collection thread is executing, the user can still interact with the
  72. system. Another technique is to run the collector when the user has
  73. been
  74. idle for X seconds. If the user has been idle so far, he is likely to
  75. remain idle. So, the collector runs while the user is making no demands
  76. on
  77. the system. If he happens to resume interating with the program in the
  78. middle of the collection, the collector detects this, and abandons the
  79. collection. The collector can then wait for the next idle period.
  80.  
  81. Other technical reasons are offered, but I'll skip them as this note is
  82. already much too long. They apply only to unusual applications that are
  83. outside the mainstream. Even most of these reasons are wrong though.
  84.  
  85. So, we're left with the cultural reasons, and these are I believe the
  86. "real" reasons why people reject garbage collection. I can shoot down
  87. incorrect technical arguments, but it's tough to dispute culture,
  88. although
  89. I sure try. The crux of cultural reason against garbage collection is
  90. that
  91. relying on a garbage collector is "sloppy". The "Code talk" note in the
  92. January Byte is a great example of this thinking. This sort of C/C++
  93. programmer believes that it is just wrong to not explicitly call
  94. delete.
  95. That if you don't, your an undisciplined hack. That if you don't,
  96. you've
  97. turned to the dark side and surely will come to a bad end.
  98.  
  99. I'm not sure how to address this. My current argument seems to get them
  100. thinking, but isn't immediately convincing. Anyone have any ideas on a
  101. better approach. Or perhaps on a better presentation. My argument is
  102. that
  103. computers are very good at mechanistic, repetitive tasks. And that
  104. people
  105. are usually pretty bad at them; being mechanistic and repetitive, these
  106. tasks are rather boring, which means that our attention often strays.
  107. People
  108. routinely turn over such tasks to computers. For example, bookkeeping
  109. or
  110. inventory control. And what else is memory deallocation but a sort of
  111. bookkeeping or inventory control. It's just that rather than being for
  112. a resource outside of the computer it is for one inside.
  113.  
  114. For anyone who may be wondering, I believe that we should use garbage
  115. collection because:
  116.     - It vastly decreases the number of bugs in programs which use
  117. it.
  118.     - It vastly decreases the time to complete programs which use
  119. it.
  120.     - It vastly increases the understandability, reuseability,
  121.         extensability, and maintainability of programs which use
  122. it.
  123.  
  124. So, do you use garbage collection in your C/C++ programs, and if not,
  125. why not?
  126.  
  127. giuliano
  128.  
  129.